<template>
  <view></view>
</template>

<script>
import { ttPay, startPay } from '@/utils/pay.js';
import { TT_PAY_STATUS, ORDER_TYPE } from '@/const/index.js';
import { getTiktokPayParamOfGoods } from '@/api/shop-activity.js';
import envVersion from '@/envVersion.js';
export default {
  data() {
    return {
      TT_PAY_STATUS,
      ORDER_TYPE,
      orderSn: '',
      // orderId: '',
      // orderToken: '',
    };
  },
  onLoad(options) {
    if (options) {
      this.orderSn = options.orderSn;
      this.getTiktokPayParam();
    }
  },
  methods: {
    getTiktokPayParam() {
      const that = this;
      // const callbackUrl = `${envVersion.h5ShopUrl}/#/pages/order/orderDetail?sn=${this.orderSn}&fromPay=1`;
      getTiktokPayParamOfGoods(this.orderSn).then((res) => {
        if (res.code === 0) {
          if (res.data.orderId) {
            startPay(res.data.orderId).then((payRes) => {
              that.handlerPayResult(payRes);
            });
          } else {
            ttPay(
              this.orderSn,
              res.data.data,
              res.data.token,
              ORDER_TYPE.MALL
            ).then((payRes) => {
              that.handlerPayResult(payRes);
            });
          }
        }
      });
    },
    handlerPayResult(res) {
      if (res.code === TT_PAY_STATUS.SUCCESS) {
        uni.navigateBack({
          delta: 1,
        });
        // uni.redirectTo({
        //   url: `/pages/web-view/index?path=${encodeURIComponent(
        //     callbackUrl
        //   )}&title=订单详情`,
        // });
      } else if (res.code === TT_PAY_STATUS.PAY_CANCEL) {
        uni.navigateBack({
          delta: 1,
        });
      } else {
        uni.navigateBack({
          delta: 1,
        });
        uni.showToast({
          title: res.msg,
          icon: 'none',
        });
      }
    },
  },
};
</script>

<style></style>
